// 保存字符串内容到文件。
// 效果相当于从浏览器下载了一个文件到本地。
// textToWrite - 要保存的文件内容
// fileNameToSaveAs - 要保存的文件名
var tools = {};

tools.saveForBrowser = function(textToWrite, fileNameToSaveAs) {
    if (cc.sys.isBrowser) {
        console.log("浏览器");
        let textFileAsBlob = new Blob([textToWrite], {type:'application/json'});
        let downloadLink = document.createElement("a");
        downloadLink.download = fileNameToSaveAs;
        downloadLink.innerHTML = "Download File";
        if (window.webkitURL != null)
        {
            // Chrome allows the link to be clicked
            // without actually adding it to the DOM.
            downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
        }
        else
        {
            // Firefox requires the link to be added to the DOM
            // before it can be clicked.
            downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
            downloadLink.onclick = destroyClickedElement;
            downloadLink.style.display = "none";
            document.body.appendChild(downloadLink);
        }
        downloadLink.click();
    }
}

module.exports = tools;